# -*- coding: utf-8 -*-
# 生成器：这种一边循环一边计算的机制，称为生成器：generator。generator保存的是算法
def fib(max):  # 斐波拉契数列
    n, a, b = 0, 0, 1
    while n < max:
        print(b)
        a, b = b, a + b
        n = n + 1
    return 'done'
print(fib(6))
# 注意，赋值语句：a, b = b, a + b
'''相当于：
    t = (b, a + b) # t是一个tuple
    a = t[0]
    b = t[1]'''
#generator生成器改写
def fib1(max):  # 斐波拉契数列
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
    return 'done'
g=fib1(7)
print(g)
print(list(g))
for n in fib1(7):#直接使用for循环来迭代
    print(n)
